package com.lzy.mall.coupon.controller;

import com.lzy.mall.common.constant.Constant;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.coupon.utils.ParamsPageUtils;
import lombok.RequiredArgsConstructor;
import com.lzy.mall.coupon.entity.SmsCoupon;
import com.lzy.mall.coupon.service.SmsCouponService;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author lzy
 * create on 2021-10-14 10:22:40
 */
@RestController
@RequestMapping("coupon")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class SmsCouponController {
    /**
     * 服务对象
     */
    private final SmsCouponService smsCouponService;

    @Value("${a.b.c}")
    private String str;

    @Value("${other.name}")
    private String str1;

    @RequestMapping("/member")
    public ResultUtils memberCoupon() {
        return ResultUtils.ok().put("coupon", "满100减99" + str + str1);
    }

    //测试
    @RequestMapping("member/list")
    public ResultUtils memberList() {
        SmsCoupon couponEntity = new SmsCoupon();
        couponEntity.setCouponName("满10减1");
        return ResultUtils.ok().put("coupons", Arrays.asList(couponEntity));
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @RequestMapping("/list")
    public ResultUtils<?> queryAll(@RequestParam Map<String, Object> params) {
        PageQuery pageQuery = ParamsPageUtils.paramsToPage(params);
        PageQuery query = smsCouponService.queryByPage(pageQuery);

        PageUtils pageUtils = new PageUtils(query);
        return ResultUtils.ok().put("page", pageUtils);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/info/{id}")
    public ResultUtils<?> queryOne(@PathVariable Long id) {
        SmsCoupon coupon = smsCouponService.queryById(id);
        return ResultUtils.ok().put("coupon", coupon);
    }

    /**
     * 新增数据
     *
     * @param smsCoupon 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    public ResultUtils<?> create(@RequestBody SmsCoupon smsCoupon) {
        smsCouponService.insert(smsCoupon);
        return ResultUtils.ok();
    }

    /**
     * 修改数据
     *
     * @param smsCoupon 实体对象
     * @return 修改结果
     */
    @RequestMapping("/update")
    public ResultUtils<?> update(@RequestBody SmsCoupon smsCoupon) {
        smsCouponService.updateById(smsCoupon);
        return ResultUtils.ok();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @RequestMapping("/delete")
    public ResultUtils<?> delete(@RequestBody List<Long> idList) {
        smsCouponService.deleteById(idList);
        return ResultUtils.ok();
    }
}
